<template>
  <div class="login">
   <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" width="300" id="Loginform">
     <el-form-item label="用户名" prop="user">
         <el-input type="text" v-model="ruleForm.user" autocomplete="off" name="userName"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off" name="passWord"></el-input>
    </el-form-item>
     <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" size="medium">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button> 
        <a href="/register" class="already">没有帐号？注册</a> 
    </el-form-item>
  </el-form>
  </div>
</template>

<script>
export default {
  data(){
      var validateuser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
       var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };

      return{
         ruleForm: { //注册登录验证
            pass: '',
            user: ''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                user: [
                    { validator: validateuser, trigger: 'blur' }
                ]
            },
      }

  },
  methods:{
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
             this.$axios.post('/api/cmsusers/login',{
                      userName :this.ruleForm.user,
                      passWord: this.ruleForm.pass
                  }).then((res)=>{
                      // console.log(res);
                      if(res.data.code == 0){
                          this.$message({
                               message: '恭喜你，登录成功',
                                type: 'success'
                          });
              
                        this.$router.push('/admin');
                        localStorage.setItem('token' , res.data.token);
                      }else{
                        this.$message({
                            message: '登录失败，请重新登录',
                            type: 'error'
                        });
                      }
                  });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }

  }
}
</script>

<style>
html{
    background: rgb(245,245,245);
}
.login{
    width: 400px; 
    margin: 100px auto;
    padding: 50px;
 
}
.already{
    margin: 10px;
}
</style>